<?php
/**
 * @Author: Marte
 * @Date:   2019-04-22 18:39:45
 * @Last Modified by:   Marte
 * @Last Modified time: 2019-04-23 16:18:43
 */
namespace app\index\controller;
use app\index\model\User as Users;
use app\common\auth\MyJwtAuth;
use app\index\controller\ResponseJson;
use think\Controller;

// 引入jwt
use think\JWT;

class User extends controller
{
    use ResponseJson;

    public function login(){
        //从客户端获取的登录参数
        $email = '123456789@qq.com';
        $password = '123456';

        // echo password_hash('123456',PASSWORD_DEFAULT);密码和散列值匹配
        // die;
        $re = Users::where('email',$email)->find();
        if(!$re){
            echo '用户不存在';die;
        }
        // password_verify — 验证密码是否和散列值匹配
        $userPasswordHash = $re['password'];
        if (!password_verify($password,$userPasswordHash)) {
            echo '密码错误';die;
        }

        /**
         * 调用MyJwtAuth类下的getJwtToken(用户id)方法 获取token
         * @var [type]
         */
        $jwtAuth = MyJwtAuth::getInstance(); // 调用单例句柄
        $token = $jwtAuth->setUid($re['id'])->getJwtToken()->getToken(); // 链式操作

        return $this->jsonSuccessData([
            'token' => $token,
        ]);
    }

    // 客户端获取用户信息接口
    public function getUser($token=[]){
        if(!$token){
            echo '请传入token';die;
        }
        $jwtAuth = MyJwtAuth::getInstance(); // 调用单例句柄
        $info = $jwtAuth->checkJwtToken($token);

        $in = json_decode($info,true);
        $user = Users::where('id',$in['uid'])->find();
        return $this->jsonSuccessData([
            'email' => $user['email'],
            'name' => $user['name'],
        ]);
    }





}
